<%--
  Created by IntelliJ IDEA.
  User: myd
  Date: 16/8/30
  Time: 上午9:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <!--支持ie7,ie8-->
    <% String path = request.getContextPath(); %>
    <meta http-equiv='X-UA-Compatible' content='IE=Edge' />
    <%--<meta http-equiv="X-UA-Compatible" content="IE=8"/>--%>
    <title>angularjs测试</title>
    <script src="<%=path%>/resource/angularjs/jquery-1.4.4.min.js"></script>
    <!--nagularjs库-->
    <script src="<%=path%>/resource/angularjs/angular.js"></script>
    <script src="<%=path%>/resource/angularjs/json3.min.js"></script>
    <script src="<%=path%>/resource/angularjs/angular-cookies.js"> </script>
    <!--支持ie7,ie8-->
    <script src="<%=path%>/resource/angularjs/angular-ie7-support.min.js"></script>
    <!--分页相关库-->
    <script src="<%=path%>/resource/angularjs/page/src/ng-pagination.js"></script>
    <link href="<%=path%>/resource/angularjs/page/src/ng-pagination.css" rel="stylesheet" />
</head>
<body id="ng-app" ng-app="userApp" ng-controller="userCtrl">
    <div>
        用户名:<input type="text" id="username" ng-model="resource.data.user_name"/>
        密码:<input type="text" id="passwd" ng-model="resource.data.user_password"/>
        性别:<select ng-model="resource.data.sex" >
            <option ng-repeat="m in sexlist"  value="{{m.name}}">{{m.name}}</option>
        </select>
        <input type="button" value="查询" ng-click="search_click()"/>
    </div>
    <div>
        <table>
            <tr>
                <td>用户名</td>
                <td>密码</td>
                <td>性别</td>
            </tr>
            <tr ng-repeat="user in userlist">
                <td>{{user.user_name}}</td>
                <td>{{user.user_password}}</td>
                <td>{{user.user_sex}}</td>
            </tr>
        </table>
         <div style="width: 100%">
            <pager page-count="pageCount" current-page="currentPage" on-page-change="onPageChange()" first-text="首页" next-text="下一页" prev-text="上一页" last-text="尾页" show-goto="true" goto-text="跳转到"></pager>
         </div>
    </div>
</body>
</html>

<script>
//    var userApp = angular.module('userApp', ['ie7-support','ng-pagination'],function($httpProvider){
//        $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
//        $httpProvider.defaults.transformRequest = function(obj){
//            var str = [];
//            for(var p in obj) {
//                str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
//            }
//            return str.join("&");
//        };
//        $httpProvider.defaults.headers.post = {
//            'Content-Type': 'application/x-www-form-urlencoded'
//        };
//    });

    var userApp = angular.module('userApp', ['ie7-support','ng-pagination']);
    userApp.controller("userCtrl", function($scope, $http, $filter,$rootScope) {

        $scope.sexlist = [
            {
                name : "男"
            },
            {
                name : "女"
            }
        ];

        $scope.data = {
            page:"1",//页码
            start:"0",//其实条数
            limit:"1",//每页条数
            user_name:"",
            user_password:"",
            user_sex:""
        };

        transFn = function(data) {
            return $.param(data);
        };
        postCfg = {
            headers : {
                'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
            },
            transformRequest : transFn
        };

        var url = "/wxapp-web/user/list.action";
        $http.post(url, $scope.data,postCfg).success(function(response) {
            console.log(response);
            $scope.userlist = response.rows;

            $scope.pageSize = response.totalCount;
            if (response.total % response.totalCount == 0) {
                $scope.pageCount  = response.total / response.totalCount;
            } else {
                $scope.pageCount  = parseInt(response.total / response.totalCount) + 1;
            }
        });

        $scope.onPageChange = function() {
            $scope.data.start= (($scope.currentPage - 1) * $scope.pageSize);
            $scope.data.page=$scope.currentPage;
            var url = "/wxapp-web/user/list.action";
            $http.post(url, $scope.data,postCfg).success(function(response) {
                console.log(response);
                $scope.userlist = response.rows;
                $scope.pageSize = response.totalCount;
                if (response.total % response.totalCount == 0) {
                    $scope.pageCount  = response.total / response.totalCount;
                } else {
                    $scope.pageCount  = parseInt(response.total / response.totalCount) + 1;
                }
            });
        }

        $scope.search_click = function() {
            //$scope.data.DTime=$scope.js_strto_times(document.getElementById("input_date").value);
            $scope.data.start=0;
            $scope.data.page=1;
            console.log($scope.data);
            var url = "/wxapp-web/user/list.action";

            $http.post(url, $scope.data,postCfg).success(function(response) {
                console.log(response);
                $scope.userlist = response.rows;
                $scope.pageSize = response.totalCount;
                if (response.total % response.totalCount == 0) {
                    $scope.pageCount  = response.total / response.totalCount;
                } else {
                    $scope.pageCount  = parseInt(response.total / response.totalCount) + 1;
                }
            });

        }

    })
</script>
